<template>
  <div class="app-container">
    <div class="custom_card_0" v-show="showSearch">
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="费用项名称" prop="feeItemName">
          <el-input
            v-model="queryParams.feeItemName"
            placeholder="请输入费用项名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>


        <el-form-item label="是否启用" prop="isAvailable">
          <el-select v-model.number="queryParams.isAvailable">
            <el-option label="启用" :value="1"></el-option>
            <el-option label="未启用" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
          >搜索
          </el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置
          </el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="custom_card">
      <el-row :gutter="10" class="mb10">
        <el-col :span="1.5">
          <el-button
            class="first-plain-btn"
            size="medium"
            icon="el-icon-plus"
            @click="handleAdd"
            v-hasPermi="['plan:items:add']"
          >新增
          </el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        :data="feeItemsList"
        @selection-change="handleSelectionChange"
        border
      >
        <el-table-column
          label="收费项名称"
          align="center"
          prop="feeItemName"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="收费项说明"
          align="center"
          prop="remark"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="收费类型"
          align="center"
          min-width="80"
          show-overflow-tooltip
        >
          <template slot-scope="{ row }">
            <dict-tag
              :options="dict.type.charge_type"
              :value="row.chargeItemsType"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="租户"
          align="center"
          prop="tenantName"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="是否启用"
          align="center"
          min-width="80"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.sys_available"
              :value="scope.row.isAvailable"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="180"
          class-name="small-padding fixed-width"
          fixed="left"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['plan:items:edit']"
            >修改
            </el-button
            >
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.row)"
              v-if="scope.row.isAvailable == 0"
              v-hasPermi="['plan:items:remove']"
            >删除
            </el-button
            >
            <el-button
              size="mini"
              type="text"
              @click="handleAvailable(scope.row, 1)"
              v-if="scope.row.isAvailable == 0"
              v-hasPermi="['store:store:edit']"
            >启用
            </el-button
            >
            <el-button
              size="mini"
              type="text"
              @click="handleAvailable(scope.row, 0)"
              v-if="scope.row.isAvailable == 1"
              v-hasPermi="['store:store:edit']"
            >停用
            </el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 添加或修改收费项对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="费用项名称" prop="feeItemName">
          <el-input
            v-model="form.feeItemName"
            placeholder="请输入费用项名称"
            clearable
            style="width: 100%"
            maxlength="20" show-word-limit
          />
        </el-form-item>

        <el-form-item label="收费类型" prop="chargeItemsType" required>
          <template v-for="dict in dict.type.charge_type">
            <el-radio :label="parseInt(dict.value)"   v-if="dict.value==1 || dict.value==2" v-model.number="form.chargeItemsType">{{dict.label}}</el-radio>
          </template>
        </el-form-item>

<!--        <el-form-item label="收费绑定项" prop="bindingCharges" required>-->
<!--          <el-radio :label="1" v-model.number="form.bindingCharges" >新增订单</el-radio>-->
<!--          <el-radio :label="2" v-model.number="form.bindingCharges" >还车验车</el-radio>-->
<!--        </el-form-item>-->

        <el-form-item label="是否启用" prop="isAvailable" required>
          <el-radio :label="1" v-model.number="form.isAvailable">启用</el-radio>
          <el-radio :label="0" v-model.number="form.isAvailable">停用</el-radio>
        </el-form-item>

        <el-form-item label="费用项说明" prop="remark">
          <el-input
            maxlength="500" show-word-limit
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listFeeItems,
  getItems,
  delItems,
  addItems,
  updateItems,
  stopUsingEnabled,
} from '@/api/charteredBus/feeItems'

export default {
  name: 'FeeItems',
  dicts: [
    'charge_type',
    'store_type',
    'sys_available',
    'store_class',
    'service_mode',
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 收费项表格数据
      feeItemsList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        feeItemName: null,
        isAvailable: null,
        deptId: null,
        tenantId: null,
      },
      numberChecked: false,
      amountChecked: false,
      showUpperLimit: true,
      // 表单参数
      form: {
        isAvailable: 1,
        chargeItemsType: 1,
        // bindingCharges: 1,
      },
      // 表单校验
      rules: {
        feeItemName: [
          {required: true, message: '请输入费用项名称', trigger: 'blur'},
        ],
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询收费项列表 */
    getList() {
      this.loading = true
      listFeeItems(this.queryParams).then((response) => {
        this.feeItemsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        feeItemId: null,
        feeItemName: null,
        isAvailable: 1,
        chargeItemsType: 1,
        // bindingCharges: 1,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        deptId: null,
        tenantId: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.feeItemId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加费用项'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const chargeItemsId = row.feeItemId || this.ids
      getItems(chargeItemsId).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改费用项'
      })
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          let formData = {
            ...this.form
          }
          if (this.form.feeItemId != null) {
            updateItems(formData).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addItems(formData).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 启用/停用 */
    handleAvailable(row, available) {
      stopUsingEnabled({
        feeItemId: row.feeItemId,
        isAvailable: available,
      }).then((response) => {
        this.$modal.msgSuccess('修改成功')
        this.open = false
        this.getList()
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const feeItemIds = row.feeItemId || this.ids
      this.$modal
        .confirm('是否确认删除数据项？')
        .then(function () {
          return delItems(feeItemIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {
        })
    },
    /** 导出按钮操作 */
    // handleExport() {
    //   const queryParams = this.queryParams
    //   this.$modal
    //     .confirm('是否确认导出所有收费项数据项？')
    //     .then(() => {
    //       this.exportLoading = true
    //       return exportItems(queryParams)
    //     })
    //     .then((response) => {
    //       this.$download.name(response.msg)
    //       this.exportLoading = false
    //     })
    //     .catch(() => {
    //     })
    // },
  },
}
</script>
